package ngames.com.sushitv.temp.Util;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.R;
import ngames.com.sushitv.Fragment.FeaturedFragment;
import ngames.com.sushitv.temp.model.SushiRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


/**
 * Created by Fearless on 2016/8/1.
 */
public class HttpUtil_featured {
    public static final String server = "http://60.191.1.98:9080/v1/";
    public static final String aboutDevice = "&device=android&api_version=1.0&app_version=1.0&token=";
    public static final String server_A_D = server + "video/rank?code=A_D&size=6&uuid=";//featured-top 全部日榜
    public static final String server_A_1 = server + "video/anime-featured?code=A_1&uuid=";//featured-anime 番剧推荐
    public static final String server_A_2 = server + "video/orig-featured?code=A_2&uuid=";//featured-anime 原创推荐
    public static final String server_H_D = server + "video/rank?code=H_D&size=6&uuid=";//Anime-top 番剧日榜
    public static final String server_H_0 = server + "video/anime-featured?code=H_0&size=6&uuid=";//Anime-New ongoing Anime 新番连载
    public static final String server_H_1 = server + "video/anime-featured?code=H_1&size=6&uuid=";//popular completed anime 完结番剧推荐
    public static String analyticalRequest(String s) {
        SushiRequest request = JSON.parseObject(s, SushiRequest.class);
        if (request.getCode().equals("0")) {
            try {
                return request.getData();
            } catch (Exception e) {

                Log.e("jsonString", e.toString());
                return null;
            }

        } else {
            return null;
        }
    }

    /**
     * 番剧日榜
     */
    public static void featured_top(Context context) {
        RequestQueue mQueue = Volley.newRequestQueue(context);
        int now = (int) (System.currentTimeMillis() / 1000);
        String A_D_url = server_A_D + AppApplication.getMyIMEI() + "&timestamp=" + now + aboutDevice;
        StringRequest stringRequest = new StringRequest(A_D_url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                String str = analyticalRequest(response);
                AppApplication.setTempStr(str);
                FeaturedFragment.featured(1);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //MyApplication.setTempStr("");
            }
        });
        mQueue.add(stringRequest);
    }

    /**
     * 番剧推荐
     */
    public static void featured_anime(Context context) {
        RequestQueue mQueue = Volley.newRequestQueue(context);
        int now = (int) (System.currentTimeMillis() / 1000);
        String A_1_url = server_A_1 + AppApplication.getMyIMEI() + "&timestamp=" + now + aboutDevice;
        StringRequest stringRequest = new StringRequest(A_1_url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    String str = analyticalRequest(response);
                    JSONObject js = new JSONObject(str);
                    JSONArray ja = js.optJSONArray("A_1");
                    AppApplication.setTempStr(ja.toString());

                    FeaturedFragment.featured(2);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //MyApplication.setTempStr("");
            }
        });
        mQueue.add(stringRequest);
    }

    /**
     * 原创推荐
     */
    public static void featured_fanMade(Context context) {
        RequestQueue mQueue = Volley.newRequestQueue(context);
        int now = (int) (System.currentTimeMillis() / 1000);
        String A_2_url = server_A_2 + AppApplication.getMyIMEI() + "&timestamp=" + now + aboutDevice;
        //Log.e("a_2",A_2_url);
        StringRequest stringRequest = new StringRequest(A_2_url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    String str = analyticalRequest(response);
                    JSONObject js = new JSONObject(str);
                    JSONArray ja = js.optJSONArray("A_2");
                    AppApplication.setTempStr(ja.toString());

                    FeaturedFragment.featured(3);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //MyApplication.setTempStr("");
            }
        });
        mQueue.add(stringRequest);
    }

    /**
     * 填装图片
     */
    public static void setImage(ImageView view, String url, Context context) {
        RequestQueue mQueue = Volley.newRequestQueue(context);
        ImageLoader imageLoader = new ImageLoader(mQueue, new ImageLoader.ImageCache() {
            @Override
            public void putBitmap(String url, Bitmap bitmap) {
            }

            @Override
            public Bitmap getBitmap(String url) {
                return null;
            }
        });
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(view, R.mipmap.ic_launcher, R.mipmap.ic_launcher);
        imageLoader.get(url, listener);
    }
}
